﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VioletDevelopmentLaboratory.Net.Web;
using VioletDevelopmentLaboratory.Net.Web.Component;

public partial class vboard_component_board_template_default_view_sub_comment : BoardSubPartBase
{
	private BoardThread CurrentThread;
	private BoardCommentCollection CurrentCommentCollection;

	private void reloadCurrentCommentCollection()
	{
		this.CurrentCommentCollection = this.CurrentThread.GetCommentCollection();
	}



	protected void Page_Init(Object sender, EventArgs e)
	{
		this.CurrentThread = (BoardThread)this.Page.Items["CurrentThread"];
		this.CurrentCommentCollection = (BoardCommentCollection)this.Page.Items["CurrentCommentCollection"];
	}

	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
			CommentRepeater_Bind();
	}

	protected void CommentRepeater_Bind()
	{
		#region List
		CommentRepeater.DataSource = this.CurrentCommentCollection;
		CommentRepeater.DataBind();
		#endregion

		#region Form
		Boolean doCommentWrite = base.CurrentUserInfo.IsAdministrator || base.CurrentUserInfo.IsSysop(base.CurrentBoard) || base.CurrentUserInfo.IsAuthorizedBy(base.CurrentBoard, BoardCommand.CommentWrite);
		CommentWriteFormPanel.Visible = base.CurrentBoard.UseComment && !this.CurrentThread.IsClosed && doCommentWrite;
		#endregion
	}

	protected void CommentRepeater_ItemDataBound(Object sender, RepeaterItemEventArgs e)
	{
		if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
		{
			BoardComment currentComment = (BoardComment)e.Item.DataItem;

			String userImageTag = GetLocalResourceObject("Share_UserImageFormat").ToString();
			String userImageUrl = VUtility.GetMemberImageUrl(currentComment.ProviderUserKey);
			Literal CommentListUserImageLiteral = (Literal)e.Item.FindControl("CommentListUserImageLiteral");
			CommentListUserImageLiteral.Text = String.Format(userImageTag, userImageUrl, userImageUrl);

			Literal ContentLiteral = (Literal)e.Item.FindControl("ContentLiteral");
			if (!currentComment.IsHtml)
				currentComment.Content = Server.HtmlEncode(currentComment.Content).Replace("\r\n", "<br />");

			ContentLiteral.Text = currentComment.Content;

			Literal DetailInfoLiteral = (Literal)e.Item.FindControl("DetailInfoLiteral");
			DetailInfoLiteral.Text = string.Format(GetLocalResourceObject("CommentList_DetailInfoFormat").ToString(), currentComment.RegistrationDate, currentComment.IP);

			if (!String.IsNullOrEmpty(currentComment.LastUpdateBy))
			{
				Literal UpdateInfoLiteral = (Literal)e.Item.FindControl("UpdateInfoLiteral");
				UpdateInfoLiteral.Text = string.Format(GetLocalResourceObject("CommentList_UpdateInfoFormat").ToString(), currentComment.LastUpdateBy, currentComment.LastUpdateDate);
			}

			#region 관리자버튼
			IButtonControl SetBlockButton = (IButtonControl)e.Item.FindControl("BlockButton");
			SetBlockButton.CommandArgument = currentComment.CommentID;

			IButtonControl ClearBlockedButton = (IButtonControl)e.Item.FindControl("ClearButton");
			ClearBlockedButton.CommandArgument = currentComment.CommentID;

			Panel AdminButtonPanel = (Panel)e.Item.FindControl("AdminButtonPanel");
			AdminButtonPanel.Visible = base.CurrentUserInfo.IsAdministrator || base.CurrentUserInfo.IsSysop(base.CurrentBoard);
			#endregion


			IButtonControl modifyButton = (IButtonControl)e.Item.FindControl("ModifyButton");
			IButtonControl DeleteButton = (IButtonControl)e.Item.FindControl("DeleteButton");

			((Control)modifyButton).Visible = false;
			((Control)DeleteButton).Visible = false;

			if (!currentComment.IsBlocked)
			{
				if ((base.CurrentUserInfo.ProviderUserKey == currentComment.ProviderUserKey) ||
					base.CurrentUserInfo.IsSysop(base.CurrentBoard) ||
					base.CurrentUserInfo.IsAdministrator)
				{
					modifyButton.CommandArgument = currentComment.CommentID;
					DeleteButton.CommandArgument = currentComment.CommentID;
					((Control)modifyButton).Visible = true;
					((Control)DeleteButton).Visible = true;
				}
			}
		}
	}
	
	protected void CommentRepeater_ItemCommand(Object source, RepeaterCommandEventArgs e)
	{
		if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
		{
			String commentID = e.CommandArgument.ToString();

			if (!VUtility.IsGuid(commentID))
			{
				ScriptManager.RegisterStartupScript(Page, this.GetType(), "Result",
					VUtility.GetMessageScript(VUtility.GetVWebGlobalResource("Board_Do_Comment_InvalidID"), true), true);
				return;
			}

			BoardComment CurrentComment = this.CurrentThread.GetComment(commentID);

			if (CurrentComment == null)
			{
				this.reloadCurrentCommentCollection();
				CommentRepeater_Bind();
				ScriptManager.RegisterStartupScript(Page, this.GetType(), "Result",
					VUtility.GetMessageScript(VUtility.GetVWebGlobalResource("Board_Do_Comment_InvalidID"), true), true);
				return;
			}

			switch (e.CommandName)
			{
				case "Edit":
					#region Edit
					CommentEditFormCommentID.Value = CurrentComment.CommentID;
					CommentEditFormTextBox.Text = CurrentComment.Content;
					CommentEditFormIsHtml.Checked = CurrentComment.IsHtml;

					ScriptManager.RegisterStartupScript(Page, this.GetType(), "Result", "Comment_Edit();", true);
					break;
					#endregion
				case "Delete":
					#region Delete
					if (base.CurrentUserInfo.IsAdministrator ||
						base.CurrentUserInfo.IsSysop(base.CurrentBoard) ||
						CurrentComment.ProviderUserKey == base.CurrentUserInfo.ProviderUserKey)
					{
						switch (CurrentComment.Remove())
						{
							case BoardCommentDeleteResult.Success:
								this.reloadCurrentCommentCollection();
								CommentRepeater_Bind();
								ScriptManager.RegisterStartupScript(Page, this.GetType(), "Result",
									VUtility.GetMessageScript(VUtility.GetVWebGlobalResource("Board_Do_CommentDelete_Success"), true), true);
								break;
							case BoardCommentDeleteResult.InvalidComment:
								ScriptManager.RegisterStartupScript(Page, this.GetType(), "Result",
									VUtility.GetMessageScript(VUtility.GetVWebGlobalResource("Board_Do_CommentDelete_Failure"), true), true);
								break;
						}
					}
					else
					{
						ScriptManager.RegisterStartupScript(Page, this.GetType(), "Result",
							VUtility.GetMessageScript(VUtility.GetVWebGlobalResource("Board_Do_CommentDelete_Rejection"), true), true);
					}
					break;
					#endregion
				case "SetBlock":
					#region SetBlock
					if (base.CurrentUserInfo.IsAdministrator || base.CurrentUserInfo.IsSysop(base.CurrentBoard))
					{
						switch (CurrentComment.SetBlock())
						{
							case BoardCommentSetBlockResult.Success:
								this.reloadCurrentCommentCollection();
								CommentRepeater_Bind();
								ScriptManager.RegisterStartupScript(Page, this.GetType(), "Result",
									VUtility.GetMessageScript(VUtility.GetVWebGlobalResource("Board_Do_CommentBlock_Success"), true), true);
								break;
							case BoardCommentSetBlockResult.InvalidComment:
								ScriptManager.RegisterStartupScript(Page, this.GetType(), "Result",
									VUtility.GetMessageScript(VUtility.GetVWebGlobalResource("Board_Do_CommentBlock_Failure"), true), true);
								break;
						}
					}
					else
					{
						ScriptManager.RegisterStartupScript(Page, this.GetType(), "Result",
							VUtility.GetMessageScript(VUtility.GetVWebGlobalResource("Board_Do_CommentBlock_Rejection"), true), true);
					}
					break;
					#endregion
				case "SetClear":
					#region SetClear
					if (base.CurrentUserInfo.IsAdministrator || base.CurrentUserInfo.IsSysop(base.CurrentBoard))
					{
						switch (CurrentComment.SetClear())
						{
							case BoardCommentSetClearResult.Success:
								this.reloadCurrentCommentCollection();
								CommentRepeater_Bind();
								ScriptManager.RegisterStartupScript(Page, this.GetType(), "Result",
									VUtility.GetMessageScript(VUtility.GetVWebGlobalResource("Board_Do_CommentClear_Success"), true), true);
								break;
							case BoardCommentSetClearResult.InvalidComment:
								ScriptManager.RegisterStartupScript(Page, this.GetType(), "Result",
									VUtility.GetMessageScript(VUtility.GetVWebGlobalResource("Board_Do_CommentClear_Failure"), true), true);
								break;
						}
					}
					else
					{
						ScriptManager.RegisterStartupScript(Page, this.GetType(), "Result",
							VUtility.GetAlertScript(VUtility.GetVWebGlobalResource("Board_Do_CommentClear_Rejection")), true);
					}
					break;
					#endregion
			}
		}
	}

	protected void AddCommentButton_Click(Object sender, EventArgs e)
	{
		if (Page.IsValid)
		{
			Boolean doCommentWrite = base.CurrentUserInfo.IsAdministrator || base.CurrentUserInfo.IsSysop(base.CurrentBoard) || base.CurrentUserInfo.IsAuthorizedBy(base.CurrentBoard, BoardCommand.CommentWrite);

			if (doCommentWrite)
			{
				switch (this.CurrentThread.AddComment(
					base.CurrentUserInfo.Name,
					base.CurrentUserInfo.ProviderUserKey,
					Request.UserHostAddress,
					CommentIsHTMLCheckBox.Checked,
					CommentContentTextBox.Text))
				{
					case BoardCommentCreateResult.Success:
						this.reloadCurrentCommentCollection();
						CommentRepeater_Bind();
						ScriptManager.RegisterStartupScript(Page, this.GetType(), "Result",
							String.Format("$get('{0}').value = '';", CommentContentTextBox.ClientID) +
							VUtility.GetMessageScript(VUtility.GetVWebGlobalResource("Board_Do_CommentWrite_Success"), true), true);
						break;
					case BoardCommentCreateResult.InvalidThread:
						ScriptManager.RegisterStartupScript(Page, this.GetType(), "Result",
							VUtility.GetMessageScript(VUtility.GetVWebGlobalResource("Board_Do_CommentWrite_Failure"), true), true);
						break;
				}
			}
			else
			{
				ScriptManager.RegisterStartupScript(Page, this.GetType(), "Result",
					VUtility.GetMessageScript(VUtility.GetVWebGlobalResource("Board_Do_CommentWrite_Rejection"), true), true);
			}
		}
	}

	protected void UpdateCommentButton_Click(Object sender, EventArgs e)
	{
		if (Page.IsValid)
		{
			if (!VUtility.IsGuid(CommentEditFormCommentID.Value))
			{
				ScriptManager.RegisterStartupScript(Page, this.GetType(), "Result",
					VUtility.GetMessageScript(VUtility.GetVWebGlobalResource("Board_Do_Comment_InvalidID"), true), true);
				return;
			}

			BoardComment CurrentComment = this.CurrentThread.GetComment(CommentEditFormCommentID.Value);

			if (CurrentComment == null)
			{
				ScriptManager.RegisterStartupScript(Page, this.GetType(), "Result",
					VUtility.GetMessageScript(VUtility.GetVWebGlobalResource("Board_Do_Comment_InvalidID"), true), true);
				return;
			}

			if (base.CurrentUserInfo.IsAdministrator ||
				base.CurrentUserInfo.IsSysop(base.CurrentBoard) ||
				CurrentComment.ProviderUserKey == base.CurrentUserInfo.ProviderUserKey)
			{
				CurrentComment.IP = Request.UserHostAddress;
				CurrentComment.LastUpdateBy = Page.User.Identity.Name;
				CurrentComment.IsHtml = CommentEditFormIsHtml.Checked;
				CurrentComment.Content = CommentEditFormTextBox.Text;

				switch (CurrentComment.Modify())
				{
					case BoardCommentUpdateResult.Success:
						this.reloadCurrentCommentCollection();
						CommentRepeater_Bind();
						ScriptManager.RegisterStartupScript(Page, this.GetType(), "CommentEdit", "Comment_Cancel();", true);
						ScriptManager.RegisterStartupScript(Page, this.GetType(), "Result",
							VUtility.GetMessageScript(VUtility.GetVWebGlobalResource("Board_Do_CommentUpdate_Success"), true), true);
						break;
					case BoardCommentUpdateResult.InvalidComment:
						ScriptManager.RegisterStartupScript(Page, this.GetType(), "Result",
							VUtility.GetMessageScript(VUtility.GetVWebGlobalResource("Board_Do_CommentUpdate_Failure"), true), true);
						break;
				}

			}
			else
			{
				ScriptManager.RegisterStartupScript(Page, this.GetType(), "Result",
					VUtility.GetMessageScript(VUtility.GetVWebGlobalResource("Board_Do_CommentUpdate_Rejection"), true), true);
			}
		}
	}
}
